package com.integral.personalAccount.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.integral.common.domain.R;
import com.integral.personalAccount.domain.PersonalAccountDo;
import com.integral.personalAccount.dao.PersonalAccountDao;
import com.integral.personalAccount.service.PersonalAccountService;
import com.integral.system.dao.SysUserDao;
import com.integral.system.dao.SysUserRoleDao;
import com.integral.system.domain.SysUserDo;
import com.integral.system.domain.SysUserRoleDo;
import com.integral.system.service.SysUserRoleService;
import com.integral.system.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class PersonalAccountServiceImpl extends ServiceImpl<PersonalAccountDao, PersonalAccountDo> implements PersonalAccountService {
    private static final String DEFAULT_PASSWORD = "wm.top1" ;//默认密码

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Override
    @Transactional
    public R add(PersonalAccountDo personalAccountDo) {
        if (ObjectUtil.isEmpty(personalAccountDo.getPwd())){
            personalAccountDo.setPwd(DEFAULT_PASSWORD);
        }

        QueryWrapper<PersonalAccountDo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_account",personalAccountDo.getUserAccount());
        List<PersonalAccountDo> list = this.list(queryWrapper);
        if (list.size() > 0){
            return R.error("当前账号已存在!");
        }

        boolean save = this.save(personalAccountDo);

        SysUserDo sysUserDo = new SysUserDo();
        sysUserDo.setUserNo(personalAccountDo.getUserNo());
        sysUserDo.setPwd(personalAccountDo.getPwd());
        sysUserDo.setUserName(personalAccountDo.getName());
        sysUserDo.setUserAccount(personalAccountDo.getUserAccount());
        sysUserDo.setAccountType("个人");

        boolean save1 = sysUserService.save(sysUserDo);

        SysUserRoleDo sysUserRoleDo = new SysUserRoleDo();
        sysUserRoleDo.setUserNo(String.valueOf(personalAccountDo.getUserNo()));
        sysUserRoleDo.setRoleNo(personalAccountDo.getPositionNo());

        boolean save2 = sysUserRoleService.save(sysUserRoleDo);

        if (save && save1 && save2 ){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @Override
    @Transactional
    public R update(PersonalAccountDo personalAccountDo) {
        boolean update = this.updateById(personalAccountDo);
        SysUserDo sysUserDo = new SysUserDo();
        sysUserDo.setUserNo(personalAccountDo.getUserNo());
        sysUserDo.setPwd(personalAccountDo.getPwd());
        sysUserDo.setUserName(personalAccountDo.getName());
        sysUserDo.setUserAccount(personalAccountDo.getUserAccount());
        sysUserDo.setAccountType("个人");

        boolean update1 = sysUserService.updateById(sysUserDo);

        QueryWrapper<SysUserRoleDo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_no",personalAccountDo.getUserNo());
        List<SysUserRoleDo> list = sysUserRoleService.list(queryWrapper);

        boolean update2 = false;
        if (list.size() == 1){
            SysUserRoleDo sysUserRoleDo = list.get(0);
            sysUserRoleDo.setRoleNo(personalAccountDo.getPositionNo());
            update2 = sysUserRoleService.updateById(sysUserRoleDo);

        }else if (list.size() == 0){ // 如果没查到就新增
            SysUserRoleDo sysUserRoleDo = new SysUserRoleDo();
            sysUserRoleDo.setUserNo(String.valueOf(personalAccountDo.getUserNo()));
            sysUserRoleDo.setRoleNo(personalAccountDo.getPositionNo());
            update2 = sysUserRoleService.updateById(sysUserRoleDo);
        }else { // 大于1条数据，报错
            log.info("***************在 用户-角色表中查询到多条数据*************");
            return R.error("同步用户职务异常!");
        }

        if (update && update1 && update2){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @Override
    public R remove(String id) {
        boolean remove = this.removeById(id);
        boolean remove1 = sysUserService.removeById(id);
        QueryWrapper<SysUserRoleDo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_no",id);
        boolean remove2 = sysUserRoleService.remove(wrapper);
        if (remove && remove1 && remove2){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @Override
    public R importData(List<PersonalAccountDo> list) {
        List<SysUserDo> users = new ArrayList<>();
        List<SysUserRoleDo> roles = new ArrayList<>();
        if (null != list && list.size() > 0) {
            boolean saveResult = false;
            boolean saveResult1 = false;
            boolean saveResult2 = false;
            try {
                //设置默认密码
                for (PersonalAccountDo personalAccountDo : list) {
                    if (ObjectUtil.isEmpty(personalAccountDo.getPwd())) {
                        personalAccountDo.setPwd(DEFAULT_PASSWORD);
                    }
                    SysUserDo sysUserDo = new SysUserDo();
                    sysUserDo.setUserNo(personalAccountDo.getUserNo());
                    sysUserDo.setPwd(personalAccountDo.getPwd());
                    sysUserDo.setUserName(personalAccountDo.getName());
                    sysUserDo.setUserAccount(personalAccountDo.getUserAccount());
                    sysUserDo.setAccountType("个人");

                    users.add(sysUserDo);

                    SysUserRoleDo sysUserRoleDo = new SysUserRoleDo();
                    sysUserRoleDo.setUserNo(String.valueOf(personalAccountDo.getUserNo()));
                    sysUserRoleDo.setRoleNo(personalAccountDo.getPositionNo());
                    roles.add(sysUserRoleDo);

                }
                saveResult = this.saveBatch(list);
                saveResult1 = sysUserService.saveBatch(users);
                saveResult2 = sysUserRoleService.saveBatch(roles);
                if (saveResult && saveResult1 && saveResult2) {
                    return R.ok();
                } else {
                    return R.error("批量导入失败，入库失败！" );
                }
            } catch (Exception e) {
                e.printStackTrace();
                return R.error("批量导入失败，服务器异常！" );
            }

        } else {
            return R.error("批量导入失败，读取文件内容为空！" );
        }
    }
}
